package dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import dao.ShopCarDao;
import dbpool.DbPool;
import entity.ShopCar;

public class ShopCarDaoImpl implements ShopCarDao{
	DbPool pool = DbPool.getPool();
	/**
	 *       	加入购物车
	 */
	@Override
	public int insertIntoShopCar(int user_id, int goods_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "insert into shopcar values(null,?,?)";
		QueryRunner runner = new QueryRunner();
		int res = runner.update(conn, sql, user_id,goods_id);
		pool.back(conn);
		return res;
	}
	/*
	 * 查询该用户购物车的所有商品id
	 */
	@Override
	public List<ShopCar> getGoodIdFromShopCar(int user_id) throws SQLException {
		Connection conn = pool.getConn();
		System.out.println(conn);
		String sql = "select goods_id from shopcar where user_id = ?";
		QueryRunner runner = new QueryRunner();
		List<ShopCar> idList = (List) runner.query(conn, sql, new BeanListHandler(ShopCar.class),user_id);
		pool.back(conn);
		return idList;
	}

	/*
	 * 根据商品id删除购物车里某件商品
	 * **/
	
	@Override
	public int delGoodsFromShopCar(int shopcar_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "delete shopcar where shopcar_id=?";
		QueryRunner runner = new QueryRunner();
		int res = runner.update(conn, sql, shopcar_id);
		pool.back(conn);
		return res;
	}

}
